package com.whz.gateway.user;

import cn.hutool.core.lang.Tuple;
import lombok.Data;

import java.util.List;
import java.util.regex.Pattern;

/**
 * 登录用户信息
 *
 * @author whz
 */
@Data
public class UserDetailInfo {
    private String userName;
    private long userId;
    private LoginInfo loginInfo;
    private List<Authority> authority;


    /**
     * 用户登录信息
     */
    @Data
    public static class LoginInfo {
        /**
         * 用户登录IP
         */
        private String ip;
        /**
         * 用户登录user agent
         */
        private String userAgent;
        /**
         * 用户设备指纹
         */
        private String deviceInfo;
    }

    /**
     * 用户权限信息
     */
    @Data
    public static class Authority {
        private static final String SPLIT = "$$";
        private static final Pattern COMPILE = Pattern.compile(SPLIT);
        /**
         * 权限所属服务的名称
         */
        private String appName;
        /**
         * 权限字符串,http method +url
         */
        private List<String> authrites;
        /**
         * 自定义权限码
         */
        private List<String> codes;

        /**
         * 权限字符串应该为http method+url格式,例如:  get&&url,post$$url
         */
        public static Tuple resolvePermission(String permission) {
            if (permission.contains(SPLIT)) {
                String[] temp = COMPILE.split(permission);
                if (temp.length > 1) {
                    return new Tuple(temp[0], temp[1]);
                }
                return new Tuple(temp[0]);
            }
            return new Tuple(permission);
        }
    }
}